<?php
namespace models\service;
use entities;

class UserB {

	public static $user;
	
	public static $errorMessage;

	public function __construct() {
	}

	public function insert() {
		
	}
	
	public static function getErrorMessage(){
		return self::$errorMessage;
	}

	public static function login($username, $password) {
		$CI =& get_instance();	
		$criteria = array('userId' => $username);
		$u = $CI->em->getRepository('\models\entities\User')->findOneBy($criteria);
		
		// get User object by username
		if ($u) {

			// this mutates (encrypts) the input password
			$u_input = new \models\entities\User;
			$u_input->setPassword($password);
			
			// password match (comparing encrypted passwords)
			if ($u->getPassword() == $u_input->getPassword()) {
				unset($u_input);
				$CI->session->set_userdata('user_id',$u->getUserId());
				self::$user = $u;

				return TRUE;
			}
			self::$errorMessage = "WRONG PASSWORD";
			unset($u_input);
		} else {
			self::$errorMessage = "NO USER DATA";	
		}		
		// login failed
		return FALSE;

	}

	public function __clone() {
		trigger_error('Clone is not allowed.', E_USER_ERROR);
	}

}